// JavaScript Document
window.onload = function () {
  var oBird = document.getElementById('bird')
  oBird.oPlay = document.getElementById('play')
  oBird.timer = null
  oBird.roadTimer = null
  oBird.zhuziTimer = null
  oBird.isStart = true // true 涓烘病寮€濮�  false涓哄紑濮�
  oBird.isAlive = true
  oBird.isAcross = true
  oBird.maxTop = 0
  oBird.iCurr = 0
  game.init(oBird)
  oFlys = document.getElementById('flys')
  document.onkeydown = function (ev) {
    var ev = ev || event
    if (ev.keyCode == 32) {
      if (oBird.isStart && oBird.isAlive) { // 娌″紑濮� 楦熸椿鐫€
        game.start(oBird)
        oFlys.play()
        // document.title = 1;
      } else if (!oBird.isStart && oBird.isAlive) { // 寮€濮嬩簡 楦熸椿鐫€
        game.fly(oBird)
        oFlys.currentTime = 0
        oFlys.play()
      } else if (oBird.isStart && !oBird.isAlive) { // 寮€濮嬩簡 楦熸浜�

      } else { // 娌″紑濮� 楦熸浜�

      }
      // alert(obj.maxTop);
      return false
    }
  }
  game.restart(oBird)
}
var game = {
  /* *
		 * init 锛� 鍒濆鍖� 瀹炵幇鍏冪礌鐨勫眳涓紝鏁板€煎垵濮嬪寲銆傝皟鐢ㄨ儗鏅Щ鍔ㄣ€侀笩鏅冨姩銆侀殢鏈洪珮搴︽煴瀛愮敓鎴愩€�
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  init: function (obj) {
    var oWrap = document.getElementById('wrap')
    var oScore1 = document.getElementById('score1')
    var oTapStart = document.getElementById('tap_start')
    var oGameoverTit = document.getElementById('gameover_tit')
    var oScoreBox = document.getElementById('score_box')
    var oMask = document.getElementById('mask')
    var oZhuziBox = document.getElementById('zhuzi')
    var aLi = oZhuziBox.getElementsByTagName('li')
    obj.iCurr = 0
    obj.isAlive = true
    obj.isStart = true
    obj.maxTop = oWrap.offsetHeight - obj.offsetHeight

    oZhuziBox.style.left = oWrap.offsetWidth + 'px'
    oWrap.maxHeight = oWrap.offsetHeight
    oScore1.innerHTML = 0
    oScore1.style.display = 'block'
    oMask.style.display = 'none'
    positionCenter(oWrap, [oScore1, oTapStart, oGameoverTit, oScoreBox, obj.oPlay ])
    obj.style.backgroundImage = 'url(http://2013.miaov.com/student/hebin/img/brid.gif)'
    obj.oPlay.style.opacity = 0
    obj.oPlay.style.filter = 'alpha(opacity=0)'
    obj.oPlay.style.display = 'none'
    oTapStart.style.opacity = 1
    oTapStart.style.filter = 'alpha(opacity=100)'
    oGameoverTit.style.opacity = 0
    oGameoverTit.style.filter = 'alpha(opacity=0)'
    oScoreBox.style.top = '621px'
    obj.style.top = '240px'
    oGameoverTit.style.top = '120px'
    game.roadMove(oWrap, obj)
    game.birdMove(obj)
    game.zhuizi(oWrap)
  },

  /* *
		 * roadMove 锛� 瀹炵幇鑳屾櫙绉诲姩
		 * params :
		 *		(object) oWrap : 鑳屾櫙鐖剁骇
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  roadMove: function (oWrap, obj) { // 閬撹矾寮€濮嬫粴鍔�  鏃犵紳婊氬姩
    var oRoad = document.getElementById('road')

    oRoad.style.width = oWrap.offsetWidth * 2 + 'px'

    obj.roadTimer = setInterval(function () {
      if (oRoad.offsetLeft < -oRoad.offsetWidth / 2) {
        oRoad.style.left = 0
      }
      oRoad.style.left = oRoad.offsetLeft - 5 + 'px'
    }, 30)
  },

  /* *
		 * birdMove 锛� 楦熷湪娓告垙寮€濮嬪墠 涓婁笅鏅冨姩
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  birdMove: function (obj) {
    var iSpeed = 0
    obj.timer = setInterval(
      function () {
        if (obj.offsetTop < 233) {
          iSpeed += (233 - obj.offsetTop) / 18
        } else {
          iSpeed -= (obj.offsetTop - 233) / 18
        }
        obj.style.top = obj.offsetTop + iSpeed + 'px'
      }
      , 30)
  },

  /* *
		 * start 锛氬紑濮嬪墠锛屾寜涓嬬┖鏍硷紝寤惰繜璋冪敤楦熻烦璺冿紝璋冪敤鏌卞瓙绉诲姩
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  start: function (obj) {
    var oTapStart = document.getElementById('tap_start')
    startMove(oTapStart, {opacity: 0}, 300, 'easeIn')
    obj.isStart = false
    game.fly(obj)
    setTimeout(game.zhuziMove, 2000, document.getElementById('bird'))
  },
  /* *
		 * fly 锛氳皟鐢ㄦ椂璺宠穬銆傚垽鏂Е搴曪紝瑙﹀簳楦熸浜°€�
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  fly: function (obj) {
    var oCrashs = document.getElementById('crashs')
    var iSpeed = -25
    var x = 3
    clearInterval(obj.timer)
    if (obj.isAlive) {
      obj.timer = setInterval(function () {
        iSpeed += x
        x *= 1.05
        var T = obj.offsetTop + iSpeed

        if (T < obj.maxTop + 4) {
          iSpeed *= 0.75
          // document.title = T;
        } else if (T < 0) { // 鏃犳硶杩涘幓
          T = 0
          // document.title = 1;
        } else {
          T = obj.maxTop + 4
          oCrashs.play()
          obj.style.backgroundImage = 'url(http://2013.miaov.com/student/hebin/img/brid.png)'
          game.gameover(obj)
        }
        obj.style.top = T + 'px'
        game.crash(obj)
        game.count(obj)
      }, 45)
    }
  },

  /* *
		 * zhuizi 锛氱敓鎴愭煴瀛愶紝璁＄畻鐖剁骇楂樺害锛岄殢鏈洪珮搴︼紝楂樺害闂撮殧涓�130
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  zhuizi: function (obj) {
    var oZhuziBox = document.getElementById('zhuzi')
    var aLi = oZhuziBox.getElementsByTagName('li')
    oZhuziBox.style.width = (aLi[0].offsetWidth + 142) * aLi.length + 'px'
    for (var i = 0; i < aLi.length; i++) {
      var aZhuzi = aLi[i].children
      var xHeight = Math.round(Math.random() * (obj.maxHeight - 130 - 40 * 2)) // 130涓轰笂涓嬫煴瀛愰棿闅� 40鏌卞瓙鏈€灏忛珮搴�
      aLi[i].style.left = i * (aLi[0].offsetWidth + 142) + 'px'
      aZhuzi[0].style.height = xHeight + 35 + 'px'
      aZhuzi[1].style.height = obj.maxHeight - 130 - 40 * 2 - xHeight + 40 + 'px'
    }
  },

  /* *
		 * zhuziMove 锛氭煴瀛愬紑濮嬬Щ鍔�
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  zhuziMove: function (obj) {
    var oZhuziBox = document.getElementById('zhuzi')
    var aLi = oZhuziBox.getElementsByTagName('li')
    if (obj.isAlive) {
      obj.zhuziTimer = setInterval(function () {
        oZhuziBox.style.left = oZhuziBox.offsetLeft - 5 + 'px'
        if (oZhuziBox.offsetLeft < -(aLi[0].offsetWidth + 142)) {
          oZhuziBox.appendChild(oZhuziBox.children[0])
          for (var i = 0; i < aLi.length; i++) {
            aLi[i].style.left = i * (aLi[0].offsetWidth + 142) + 'px'
          }
          oZhuziBox.style.left = aLi[0].offsetWidth + 142 + oZhuziBox.offsetLeft + 'px'
        }
      }, 30)
    }
  },
  /* *
		 * crash 锛氶笩璺熸煴瀛愮殑纰版挒妫€娴嬶紝纰版挒鍚庢墽琛� game.fall()缁撴潫娓告垙 锛屾湭纰版挒count() 缁熻
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  crash: function (obj) {
    var oZhuziBox = document.getElementById('zhuzi')
    var aLi = oZhuziBox.getElementsByTagName('li')
    var oGolds = document.getElementById('golds')
    var oCrashs = document.getElementById('crashs')
    var iWidth = aLi[0].offsetWidth + 142 + oZhuziBox.offsetLeft
    var T1 = obj.offsetTop
    var B1 = obj.offsetTop + obj.offsetHeight - 2
    var R1 = obj.offsetLeft + obj.offsetWidth - 1
    var L1 = obj.offsetLeft + 2
    //
    var L2 = 0
    var B2 = 0
    var T2 = 0
    var R2 = 0
    if (obj.iCurr > 0) { // 纭畾妫€娴嬬殑鍊�
      L2 = aLi[1].offsetLeft + oZhuziBox.offsetLeft + 20
      B2 = aLi[1].children[0].offsetHeight
      T2 = aLi[1].children[1].offsetTop + 20
    } else {
      L2 = oZhuziBox.offsetLeft + 20
      B2 = aLi[0].children[0].offsetHeight
      T2 = aLi[0].children[1].offsetTop + 20
    }
    R2 = L2 + aLi[0].offsetWidth - 20
    var x = L1 - R2 // ((aLi[0].offsetWidth)*2+142-Math.abs(oZhuziBox.offsetLeft))
    if (L1 > R2 && iWidth > L1 && obj.isAcross && obj.iCurr == 0) {
      obj.iCurr = 1
      obj.isAcross = false
      oGolds.play()
    }
    if (L1 > L2 && L1 < R2) {
      obj.isAcross = true
    }
    if (L1 > R2 && obj.iCurr > 0 && obj.isAcross) {
      obj.iCurr++
      obj.isAcross = false
      oGolds.play()
    }
    // document.title = obj.iCurr;
    // console.log(L1+':'+R2+':'+iWidth+':'+obj.isAcross+':'+obj.iCurr+':'+aLi[0].n);
    if ((R1 > L2 && T1 < B2) || (B1 > T2 && R1 > L2)) {
      obj.style.backgroundImage = 'url(http://2013.miaov.com/student/hebin/img/brid.png)'
      oCrashs.play()
      game.fall(obj)
    }
  },
  /* *
		 * count 锛� 閫氳繃 iCurr 缁熻鍒嗘暟
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  count: function (obj) {
    var oScore = document.getElementById('score1')
    oScore.innerHTML = obj.iCurr
  }, /* *
		 * fall 锛氱鍒版煴瀛� 鏃犳硶璺宠穬 寮€濮嬪潬钀�
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */

  fall: function (obj) {
    obj.src = 'http://2013.miaov.com/student/hebin/img/brid.png'
    game.gameover(obj)
    setTimeout(obj.timer = function () {
      startMove(obj, {top: obj.maxTop + 4}, 400, 'easeInStrong')
    }, 300)
  },
  /* *
		 * gameover 锛� 楦熸浜★紝 娓告垙缁撴潫 娓呴櫎瀹氭椂鍣ㄣ€傛樉绀虹浉搴斿竷灞€
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  gameover: function (obj) { // 娓告垙缁撴潫 鏄剧ず淇℃伅
    var oGameoverTit = document.getElementById('gameover_tit')
    var oScoreBox = document.getElementById('score_box')
    var img = oScoreBox.getElementsByTagName('img')[0]
    var oScore = document.getElementById('score1')
    var oCScore = document.getElementById('curr_score')
    var oBScore = document.getElementById('best_score')
    var oStarts = document.getElementById('starts')
    var N = 0
    var M = 0
    oCScore.innerHTML = 0
    if (getCookie('score')) {
      M = getCookie('score')
      oBScore.innerHTML = M
      if (M < obj.iCurr) {
        M = obj.iCurr
        setCookie('score', M, 5)
      }
      // alert(getCookie('score'))
    } else {
      setCookie('score', obj.iCurr, 5)
    }
    obj.src = 'http://2013.miaov.com/student/hebin/img/brid.png'
    obj.isAlive = false
    obj.isStart = true
    oScore.style.display = 'none'
    clearInterval(obj.timer)
    clearInterval(obj.roadTimer)
    clearInterval(obj.zhuziTimer)
    setTimeout(function () {
      startMove(oGameoverTit, {opacity: 100, top: oGameoverTit.offsetTop + 20}, 400, 'elasticOut')
    }, 200)
    setTimeout(function () {
      startMove(oScoreBox, {top: 225}, 300, 'easeOut', function () {
        oStarts.play()
        oStarts.currentTime = 0
        var timer = setInterval(function () {
          oCScore.innerHTML = N
          if (N == obj.iCurr) {
            clearInterval(timer)
          }
          N++
        }, 50)
        obj.oPlay.style.display = 'block'
        startMove(obj.oPlay, {opacity: 100}, 100, 'easeIn')
      })
    }, 800)
  },
  /* *
		 * restart 锛氱偣鍑绘寜閽噸鏂板紑濮嬶紝闂幇閬僵锛岃皟鐢ㄥ垵濮嬪寲銆�
		 * params :
		 *		(object) 楦�
		 * return
		 *		void
		 * */
  restart: function (obj) { // 鐐瑰嚮閲嶆柊寮€濮�
    var oMask = document.getElementById('mask')
    var oStarts = document.getElementById('starts')
    obj.oPlay.onclick = function () {
      oMask.style.display = 'block'
      oStarts.play()
      startMove(oMask, {opacity: 100}, 300, 'linear', function () {
        game.init(obj)
        startMove(oMask, {opacity: 0}, 300, 'linear')
      })
    }
  }
}
